package com.macro.mall.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.portal.domain.Partner;
import com.macro.mall.portal.domain.User;
import com.macro.mall.portal.domain.vo.PageVo;
import com.macro.mall.portal.domain.vo.WxMsgVo;
import com.macro.mall.portal.dto.PartnerDto;
import com.macro.mall.portal.service.IPartnerService;
import com.macro.mall.portal.service.IUserService;
import com.macro.mall.portal.util.ConfigConstant;
import com.macro.mall.portal.util.ShareCodeUtils;
import com.macro.mall.portal.util.WxMsgPush;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 合伙人申请记录表 前端控制器
 *
 * @author
 * @since 2021-09-11
 */
@RestController
@RequestMapping("/partner")
@Api(tags = "合伙人申请", description = "合伙人申请")
public class PartnerController {

    @Autowired
    private IPartnerService partnerService;

    @Autowired
    private WxMsgPush wxMsgPush;

    @Autowired
    private IUserService vxUserService;

    @ApiOperation("合伙人申请")
    @Transactional
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult add(@RequestBody Partner partner) {
        try {

            partner.setPartnerStatus(1);
            partnerService.save(partner);

            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("phone", partner.getPhone());
            User one = vxUserService.getOne(wrapper);

            if (one != null) {
                if (one.getSuperId() != 0) {
                    long a = 201;
                    return CommonResult.failed(a, "用户已经存在上级");
                }
            }
            if (one == null) {
                //todo 注册用户
                User vxUser = User.builder()
                        .username("微信用户")
                        .phone(partner.getPhone())
                        .account(partner.getName())
                        .build();
                vxUserService.save(vxUser);
                User user = vxUser.toBuilder().shareCode(ShareCodeUtils.createCode(vxUser.getId())).build();
                vxUserService.updateById(user);
            }

            //todo 文案
            String url = ConfigConstant.HHR_URL + "?id=" + partner.getId();
            LocalDateTime time = LocalDateTime.now();

            DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            String now = dtf2.format(time);
            WxMsgVo msgVo = WxMsgVo.builder()
                    .first("您好！下级合伙人申请已经提交")
                    .keyword1(partner.getName() + "｜" + partner.getPhone())
                    .keyword2(now)
                    .openId(partner.getOpenId())
                    .url(url)
                    .remark("点击查看详情，查看审核进展！")
                    .build();
            wxMsgPush.SendWxMsg(msgVo);
            return CommonResult.success(null, "提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("查看下级合伙人-专员-申请记录")
    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    public CommonResult pageList(@RequestBody PartnerDto dto) {
        try {
            PageHelper.startPage(dto.getPage(), dto.getSize());
            QueryWrapper<Partner> wrapper = new QueryWrapper<>();
            wrapper.eq("open_id", dto.getOpenId());
            if (!StringUtils.isEmpty(dto.getPartnerStatus())) {
                wrapper.eq("partner_status", dto.getPartnerStatus());
            }
            List<Partner> partnerList = partnerService.list(wrapper);
            PageInfo<Partner> pageInfo = new PageInfo<>(partnerList, dto.getSize());
            return CommonResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("合伙人查询进展")
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public CommonResult getInfo(@RequestBody Partner partner) {
        try {
            QueryWrapper<Partner> wrapper = new QueryWrapper<>();
            wrapper.eq("id", partner.getId());
            Partner one = partnerService.getOne(wrapper);
            return CommonResult.success(one);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("用户查看下级合伙人记录")
    @RequestMapping(value = "/getRecord", method = RequestMethod.POST)
    public CommonResult getRecord(@RequestBody PageVo dto) {
        try {
            PageHelper.startPage(dto.getPage(), dto.getSize());
            QueryWrapper<Partner> wrapper = new QueryWrapper<>();
            wrapper.eq("phone", dto.getPhone());
            List<Partner> partnerList = partnerService.list(wrapper);
            PageInfo<Partner> pageInfo = new PageInfo<>(partnerList, dto.getSize());
            return CommonResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

}
